package com.wangyang.stock.business.services;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.wangyang.stock.business.IInvoiceService;
import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.po.Employee;
import com.wangyang.stock.po.InOut;
import com.wangyang.stock.po.Invoice;
import com.wangyang.stock.po.InvoiceType;
import com.wangyang.stock.po.MerInv;
import com.wangyang.stock.po.Merchandise;
import com.wangyang.stock.po.Storehouse;
import com.wangyang.stock.util.DateTools;
import com.wangyang.stock.vo.InvoiceVo;

public class InvoiceServiceImp implements IInvoiceService {
	private IDao dao;

	public IDao getDao() {
		return dao;
	}

	public void setDao(IDao dao) {
		this.dao = dao;
	}

	public boolean createInvoice(InvoiceVo invoiceVo) {

		Invoice invoice = null;
		Object obj = null;
		String[][] mers = null;
		MerInv merInv = null;
		int maxid = 0;

		boolean is = false;
		if (1 == invoiceVo.getInvoiceTypeId()) {
			invoice = new Invoice();
			invoice.setInOut(new InOut(invoiceVo.getInOutId()));
			invoice.setStorehouse(new Storehouse(invoiceVo.getStorehouseId()));
			invoice.setInvoiceDate(DateTools.stringToDate(invoiceVo.getInvoiceDate(), "yyyy-MM-dd"));
			invoice.setInvoiceTotal(invoiceVo.getInvoiceTotal());
			invoice.setEmployee(new Employee(invoiceVo.getEmployeeId()));
			invoice.setInvoiceCid(invoiceVo.getInvoiceCid());
			invoice.setInvoiceMemo(invoiceVo.getInvoiceMemo());
			invoice.setInvoiceType(new InvoiceType(invoiceVo.getInvoiceTypeId()));

			obj = dao.select("select max(inv.invoiceId) from Invoice inv").get(0);
			if (null == obj) {
				maxid = 1;
			} else {
				maxid = (Integer) obj + 1;
			}

			invoice.setInvoiceId(maxid);

			mers = invoiceVo.getMers();

			if (dao.create(invoice)) {
				for (int i = 0; i < mers.length; i++) {
					int merchandiseId = Integer.parseInt(mers[i][0]);
					float price = Float.parseFloat(mers[i][1]);
					int amount = Integer.parseInt(mers[i][2]);
					int oldAmount = Integer.parseInt(mers[i][3]);
					float totalPrice = Float.parseFloat(mers[i][4]);

					Merchandise merchandise = (Merchandise) dao.select("from Merchandise mer where mer.merchandiseId = " + merchandiseId).get(0);
					merchandise.setMerchandiseAmount(amount + oldAmount);
					merchandise.setMerchandiseInPrice(price);

					Set storehousesSet = new HashSet();
					storehousesSet.add(new Storehouse(invoiceVo.getStorehouseId()));
					merchandise.setStorehouses(storehousesSet);

					merInv = new MerInv();
					merInv.setInvoice(invoice);
					merInv.setMerchandise(merchandise);
					merInv.setMerchandisePrice(price);
					merInv.setMerchandiseAmount(amount);
					merInv.setMerchandiseTotal(totalPrice);
					if (dao.create(merInv)) {
						is = true;
					}
				}
			}

		}
		if (2 == invoiceVo.getInvoiceTypeId()) {
			invoice = new Invoice();
			invoice.setInvoiceDate(DateTools.stringToDate(invoiceVo.getInvoiceDate(), "yyyy-MM-dd"));
			invoice.setInvoiceTotal(invoiceVo.getInvoiceTotal());
			invoice.setEmployee(new Employee(invoiceVo.getEmployeeId()));
			invoice.setInvoiceCid(invoiceVo.getInvoiceCid());
			invoice.setInvoiceMemo(invoiceVo.getInvoiceMemo());
			invoice
					.setInvoiceType(new InvoiceType(invoiceVo
							.getInvoiceTypeId()));

			obj = dao.select("select max(inv.invoiceId) from Invoice inv").get(
					0);
			if (null == obj) {
				maxid = 1;
			} else {
				maxid = (Integer) obj + 1;
			}

			invoice.setInvoiceId(maxid);

			mers = invoiceVo.getMers();

			if (dao.create(invoice)) {
				for (int i = 0; i < mers.length; i++) {
					int merchandiseId = Integer.parseInt(mers[i][0]);
					int amount = Integer.parseInt(mers[i][1]);
					int oldAmount = Integer.parseInt(mers[i][2]);

					Merchandise merchandise = (Merchandise) dao.select(
							"from Merchandise mer where mer.merchandiseId = "
									+ merchandiseId).get(0);
					merchandise.setMerchandiseAmount(amount + oldAmount);

					Set storehousesSet = new HashSet();
					storehousesSet.add(new Storehouse(invoiceVo
							.getStorehouseId()));
					merchandise.setStorehouses(storehousesSet);

					merInv = new MerInv();
					merInv.setInvoice(invoice);
					merInv.setMerchandise(merchandise);
					merInv.setMerchandiseAmount(amount);
					if (dao.create(merInv)) {
						is = true;
					}
				}
			}
		}
		return is;
	}

	public List selectInvoice(String invCid, int typeId, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub

		StringBuffer hql = new StringBuffer("from Invoice inv where 1=1");
		if (null != invCid && !"".equals(invCid)) {
			hql.append(" and inv.invoiceCid = ");
			hql.append(invCid);
		}
		if (0 != typeId) {
			hql.append(" and inv.invoiceType.invoiceTypeId = ");
			hql.append(typeId);
		}
		if (null != beginTime && !"".equals(beginTime) && null != endTime
				&& !"".equals(endTime)) {
			hql.append(" and inv.invoiceDate >= '");
			hql.append(beginTime);
			hql.append("' and inv.invoiceDate <= '");
			hql.append(endTime);
			hql.append("'");
		}

		System.out.println(hql);

		List list = dao.select(hql.toString());
		List voList = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Invoice inv = (Invoice) list.get(i);
			InvoiceVo invVo = new InvoiceVo();
			invVo.setInvoiceId(inv.getInvoiceId());
			invVo.setInvoiceCid(inv.getInvoiceCid());
			invVo.setInvoiceTypeName(inv.getInvoiceType().getInvoiceTypeName());
			invVo.setEmployeeName(inv.getEmployee().getEmployeeName());
			invVo.setInvoiceDate(DateTools.dateToString(new Date(inv.getInvoiceDate().getTime()), "yyyy-MM-dd"));
			invVo.setInvoiceTotal(inv.getInvoiceTotal());
			invVo.setInvoiceMemo(inv.getInvoiceMemo());
			invVo.setInvoiceTypeName(inv.getInvoiceType().getInvoiceTypeName());
			invVo.setEmployeeName(inv.getEmployee().getEmployeeName());
			if (null != inv.getStorehouse()) {
				invVo.setStorehouseName(inv.getStorehouse().getStorehouseName());
			}
			if (null != inv.getInOut()) {
				invVo.setInOutName(inv.getInOut().getInoutCompanyName());
			}
			if (null != inv.getOperator()) {
				invVo.setOperatorEmpName(inv.getOperator().getEmployee().getEmployeeName());
			}
			voList.add(invVo);
		}
		return voList;
	}
}
